package com.iqik.sc.controller;

import com.iqik.sc.pojo.Token;
import com.iqik.sc.pojo.User;
import com.iqik.sc.ns.service.AuthCodeService;
import com.iqik.sc.ns.service.TokenService;
import com.iqik.sc.ns.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private AuthCodeService authCodeService;

    @GetMapping("/login/{email}/{password}")
    public String login(HttpServletResponse response,
                        @PathVariable(value = "email") String email,
                        @PathVariable(value = "password") String password) {

        User user = userService.login(email, password);
        if (user == null) {
            return "Nothing";
        }
        Cookie cookie = new Cookie("token",tokenService.createToken(email).getToken());
        cookie.setPath("/");
        response.addCookie(cookie);
        return user.getEmail();
    }

    @RequestMapping("/register/{email}/{password}/{code}")
    public String register(HttpServletResponse response,
                         @PathVariable(value = "email") String email,
                         @PathVariable(value = "password") String password,
                         @PathVariable(value = "code") String code) {
        // TODO 注册接⼝，true成功，false失败
        String msgCode = authCodeService.verityCode(email, code);
        switch (msgCode) {
            case "0":
                User user = userService.registerUser(email, password);
                Cookie cookie = new Cookie("token",tokenService.createToken(email).getToken());
                cookie.setPath("/");
                return "{\"status\": \"true\"}";
            case "2":
                return "{\"status\": \"overdue\"}";
            case "1":
            default:
                return "{\"status\": \"false\"}";
        }
    }

    /**
     * TODO /user/isRegistered/{email}
     * 是否已注册，根据邮箱判断,true代表已经注册过，
     * false代表尚未注册
     */
    @RequestMapping("/isRegistered/{email}")
    public String isRegister(@PathVariable(value = "email") String email) {
        System.out.println("/isRegistered/" + email);
        return String.valueOf(userService.hadUser(email));
    }

    /**
     * TODO /user/info/{token} 根据token查询⽤户登录邮箱接⼝
     */
    @RequestMapping("/info/{token}")
    public String info(@PathVariable(value = "token") String token) {
        Token tokenByEmail = tokenService.getTokenByToken(token);
        if (tokenByEmail != null) {
            return tokenByEmail.getEmail();
        }
        return "Nothing";
    }

}
